package com.nobuyme.box.account;


import android.text.TextUtils;

import com.google.gson.annotations.SerializedName;
import com.nobuyme.box.app.MyApplication;
import com.nobuyme.box.app.SpDao;
import com.pachong.android.frameworkbase.utils.GsonUtils;
import com.pachong.android.frameworkbase.utils.io.SharePreferenceUtil;


/**
 * 账号类
 * <p>
 * 数据直接写到sp里，从内存中读取
 * <p>
 *
 *
 * @Author: PV
 * @E-mail: wqy@sina.cn
 * @CreateDate: 2017/04/06 16:35
 * @Version: 1.0.0
 */
public class Account {

    private static final String SP_FILENAME_ACCOUNT = Account.class.getSimpleName();
    private static final String KEY_ID ="id";
    private static final String KEY_POLLING_ONE ="polling_one";
    private static final String KEY_POLLING_TWO ="polling_two";
    private static final String KEY_POLLING_THREE ="polling_three";
    private static final String KEY_POLLING_FOUR ="polling_four";
    private static final String KEY_TOYBOX_CODE ="toybox_code";
    private static final String KEY_NAME ="name";
    private static final String KEY_IS_LOGIN = "is_login";
    private static final String KEY_IS_SUCCESS = "is_success";
    private static final String KEY_IS_UPDATA = "is_updata";

    @SerializedName("id")
    private int mId;
    @SerializedName("polling_one")
    private int mPolling_one;
    @SerializedName("polling_two")
    private int mPolling_two;
    @SerializedName("polling_three")
    private int mPolling_three;
    @SerializedName("polling_four")
    private int mPolling_four;
    @SerializedName("toybox_code")
    private String mToybox_code;
    @SerializedName("name")
    private String mName;
    @SerializedName("is_login")
    private boolean mIs_login;
    @SerializedName("is_success")
    private boolean mIs_success;
    @SerializedName("is_updata")
    private boolean mIs_updata;


    public Account() {
    }
    /**
     * 初始化，从SP中读取
     */
    public static Account createFromSp() {
//        String acctJson = SpDao.getCurrentAccount(MyApplication.getAppContext());
//        return GsonUtils.string2Object(acctJson, Account.class);
        Account account = new Account();
        account.setId(getInt(KEY_ID));
        account.setToybox_code(getString(KEY_TOYBOX_CODE));
        account.setName(getString(KEY_NAME));
//        account.setPolling_one(getInt(KEY_POLLING_ONE));
//        account.setPolling_two(getInt(KEY_POLLING_TWO));
//        account.setPolling_three(getInt(KEY_POLLING_THREE));
//        account.setPolling_four(getInt(KEY_POLLING_FOUR));
        return account;
    }

    /**
     * 初始化, 克隆一个对象
     */
    public static Account createFrom(Account acct) {
        Account account = new Account();
        account.setId(acct.getId());
        account.setToybox_code(acct.getToybox_code());
        account.setName(acct.getName());
//        account.setPolling_one(acct.getPolling_one());
//        account.setPolling_two(acct.getPolling_two());
//        account.setPolling_three(acct.getPolling_three());
//        account.setPolling_four(acct.getPolling_four());
        return account;
    }

    public void save2Sp() {
        String acctJson = GsonUtils.object2String(this);
        SpDao.setCurrentAccount(MyApplication.getAppContext(), acctJson);
    }



    public boolean getIs_updata() {
        return mIs_updata;
    }

    public void setIs_updata(boolean is_updata) {
        if (!(mIs_updata == is_updata)) {
            mIs_updata = is_updata;
            saveBoolean(KEY_IS_UPDATA, mIs_updata);
        }
    }

    public boolean getIs_success() {
        return mIs_success;
    }

    public void setIs_success(boolean is_success) {
        if (!(mIs_success == is_success)) {
            mIs_success = is_success;
            saveBoolean(KEY_IS_SUCCESS, mIs_success);
        }
    }


    public boolean getIs_login() {
        return mIs_login;
    }

    public void setIs_login(boolean is_login) {
        if (!(mIs_login == is_login)) {
            mIs_login = is_login;
            saveBoolean(KEY_IS_LOGIN, mIs_login);
        }
    }


    public String getName() {
        return mName;
    }

    public void setName(String name) {
        mName = name;
        saveString(KEY_NAME, mName);
    }

    public String getToybox_code() {
        return mToybox_code;
    }

    public void setToybox_code(String toybox_code) {
        if (!TextUtils.equals(this.mToybox_code, toybox_code)) {
            mToybox_code = toybox_code;
            saveString(KEY_TOYBOX_CODE, mToybox_code);
        }
    }

    public int getId() {
        return mId;
    }

    public void setId(int id) {
            mId = id;
            saveInt(KEY_ID, mId);
    }
    public int getPolling_one() {
        return mPolling_one;
    }
    public void setPolling_one(int polling_one) {
        mPolling_one = polling_one;
        saveInt(KEY_POLLING_ONE, mPolling_one);
    }

    public int getPolling_two() {
        return mPolling_two;
    }
    public void setPolling_two(int polling_two) {
        mPolling_two = polling_two;
        saveInt(KEY_POLLING_TWO, mPolling_two);
    }

    public int getPolling_three() {
        return mPolling_three;
    }
    public void setPolling_three(int polling_three) {
        mPolling_three = polling_three;
        saveInt(KEY_POLLING_THREE, mPolling_three);
    }
    public int getPolling_four() {
        return mPolling_four;
    }

    public void setPolling_four(int polling_four) {
        mPolling_four = polling_four;
        saveInt(KEY_POLLING_FOUR, mPolling_four);
    }

    private static void saveString(String key, String value) {
        SharePreferenceUtil.saveString(MyApplication.getAppContext(), SP_FILENAME_ACCOUNT, key, value);
    }

    private static String getString(String key) {
        return SharePreferenceUtil.getString(MyApplication.getAppContext(), SP_FILENAME_ACCOUNT, key);
    }

    private static void saveBoolean(String key, Boolean value) {
        SharePreferenceUtil.saveBoolean(MyApplication.getAppContext(), SP_FILENAME_ACCOUNT, key, value);
    }

    private static Boolean getBoolean(String key) {
        return SharePreferenceUtil.getBoolean(MyApplication.getAppContext(), SP_FILENAME_ACCOUNT, key, false);
    }

    private static void saveInt(String key, int value) {
        SharePreferenceUtil.saveInt(MyApplication.getAppContext(), SP_FILENAME_ACCOUNT, key, value);
    }

    private static int getInt(String key) {
        return SharePreferenceUtil.getInt(MyApplication.getAppContext(), SP_FILENAME_ACCOUNT, key);
    }

    @Override
    public String toString() {
        return "Account{" +
                "mId=" + mId +
                ", mToybox_code=" + mToybox_code +
                ", mName='" + mName + '\'' +
                '}';
    }
}
